package com.hhss.qishi.config.runner;

import com.hhss.qishi.utils.FileUtil;
import com.hhss.qishi.utils.OSUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;

/**
 * 检查文件，应用启动时执行
 * @Author :Administrator
 * @Description
 * @Date :2019-06-06 10:09.
 */
@Component
public class CheckFileRunner implements ApplicationRunner, Order {

	@Value("${QSApplication.annex.uploadVirtualPath}")
	private String UPLOAD_VIRTUAL_PATH;

	private String UEDITOR_PATH = "ueditor\\";

	@Override
	public Class<? extends Annotation> annotationType() {
		return null;
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		checkUeditorConfig();
	}

	@Override
	public int value() {
		return 0;
	}

	/**
	 * 检查ueditor的配置文件config.json是否正常
	 */
	private void checkUeditorConfig(){
		String configName = "config.json";
		// config.json应在文件上传目录下
		String congfigPath = UPLOAD_VIRTUAL_PATH + UEDITOR_PATH + configName;
		congfigPath = FileUtil.compatiblePath(congfigPath);
		File configFile = new File(congfigPath);

		try {
			FileUtil.checkDir(configFile);

			String rootPath = this.getClass().getResource("/").getPath();
			if(OSUtil.isWindows()){
				rootPath = rootPath.replaceAll("^\\/", "");
			}
			String filePath = rootPath + "js/ueditor/jsp/config.json";
			filePath = FileUtil.compatiblePath(filePath);
			File file = new File(filePath);
			FileUtil.copyFile(file, configFile);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
